package com.zznj.xzjc.activity.index.push;

import android.content.Context;
import android.graphics.Typeface;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.google.gson.Gson;
import com.zznj.xzjc.R;
import com.zznj.xzjc.base.BaseActivity;
import com.zznj.xzjc.fragment.index.PushFragment;
import com.zznj.xzjc.net.SharedHelper;
import com.zznj.xzjc.net.entity.DictionaryBean;
import com.zznj.xzjc.net.entity.LoginBean;
import com.zznj.xzjc.view.StatusBarUtil;
import com.zznj.xzjc.widget.FragmentAda;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 描述：线索发现
 */
public class ActivityPushMain extends BaseActivity {
    @BindView(R.id.backImage)
    View backImage;
    @BindView(R.id.stl_main)
    SlidingTabLayout stlMain;
    @BindView(R.id.vp)
    ViewPager vp;

    Unbinder unbinder;

    @BindView(R.id.titleLinear)
    LinearLayout titleLinear;
    @BindView(R.id.searchEdit)
    EditText searchEdit;
    @BindView(R.id.searchBtn)
    ImageView searchBtn;

    @BindView(R.id.xxlxLinear)
    View xxlxLinear;
    @BindView(R.id.selectTv)
    TextView selectTv;
    @BindView(R.id.selectTv_xx)
    ImageView selectTv_xx;


    @BindView(R.id.zheCeng)
    View zheCeng;
    @BindView(R.id.selectLinear)
    View selectLinear;

    @BindView(R.id.img1)
    View img1;
    @BindView(R.id.img2)
    View img2;
    @BindView(R.id.img3)
    View img3;



    @BindView(R.id.msgAll)
    TextView msgAll;
    @BindView(R.id.msgWd)
    TextView msgWd;
    @BindView(R.id.msgYd)
    TextView msgYd;

    @Override
    protected int getLayoutId() {
        return R.layout.gyss_activity_push_main_layout;
    }

    private LoginBean user;
    private int selectIndex = 0;
    private String key;
    private String state = "";

    @Override
    public void initView() {
        selectIndex = getIntent().getExtras().getInt("index");
        key = getIntent().getExtras().getString("key");
        user = new Gson().fromJson(SharedHelper.readValueByKeyForString(mContext, "Member"), LoginBean.class);
        searchEdit.setText(key);
        setTitle("案源库");
        init();
    }


    //policyIssued(政策发布),typicalCase(典型案例),notice(通知公告),clueAssign(指定管辖),knowledgeShare(知识共享)
    private String[] mTitle = {"全部", "指定管辖", "政策发布", "典型案例", "通知公告"};
    private String[] mValue = {"", "clueAssign", "policyIssued", "typicalCase", "notice"};
    private ArrayList<Fragment> arrayList = null;

    private void init() {

        /***
         * 监听输入框是否点击了搜索
         */
        searchEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH) {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);//关闭软键盘
                    ((PushFragment) arrayList.get(vp.getCurrentItem())).search();
                    return true;
                }
                return false;
            }
        });

        arrayList = new ArrayList<>();
        for (int i = 0; i < mTitle.length; i++) {

        }

        stlMain.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                selectIndex = position;
                for (int i = 0; i < arrayList.size(); i++) {
                    if (i == position) {
                        stlMain.getTitleView(position).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    } else {
                        stlMain.getTitleView(i).setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    }
                }

            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        vp.setAdapter(new FragmentAda(getSupportFragmentManager(), arrayList, null));
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectIndex = position;
                for (int i = 0; i < arrayList.size(); i++) {
                    if (i == position) {
                        stlMain.getTitleView(position).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    } else {
                        stlMain.getTitleView(i).setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        stlMain.setViewPager(vp, mTitle);


        stlMain.getTitleView(selectIndex).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        vp.setCurrentItem(selectIndex);
    }

    private List<DictionaryBean> fromList;

    @Override
    protected void initData() {
        titleLinear.setPadding(0, StatusBarUtil.getStatusBarHeight(mContext), 0, 0);
    }

    @OnClick({R.id.backImage, R.id.searchBtn, R.id.zheCeng, R.id.selectTv, R.id.msgAll, R.id.msgWd, R.id.msgYd})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.backImage) {
            finish();
        } else if (view.getId() == R.id.searchBtn) {
            key = searchEdit.getText().toString();
            ((PushFragment) arrayList.get(vp.getCurrentItem())).search();
        } else if (view.getId() == R.id.zheCeng) {
            zheCeng.setVisibility(View.GONE);
            selectLinear.setVisibility(View.GONE);
            selectTv.setTextColor(getResources().getColor(R.color.tab_menu_text_normal));
            selectTv_xx.setImageResource(R.mipmap.gyss_xiala_img);
        } else if (view.getId() == R.id.selectTv) {
            if(zheCeng.getVisibility() == View.VISIBLE){
                zheCeng.setVisibility(View.GONE);
                selectLinear.setVisibility(View.GONE);
                selectTv.setTextColor(getResources().getColor(R.color.tab_menu_text_normal));
                selectTv_xx.setImageResource(R.mipmap.gyss_xiala_img);
                return;
            }
            zheCeng.setVisibility(View.VISIBLE);
            selectLinear.setVisibility(View.VISIBLE);
            selectTv.setTextColor(getResources().getColor(R.color.tab_menu_text_select));
            selectTv_xx.setImageResource(R.mipmap.gyss_xiangxia_select);
        } else {
            zheCeng.setVisibility(View.GONE);
            selectLinear.setVisibility(View.GONE);
            selectTv.setTextColor(getResources().getColor(R.color.color_333333));
            selectTv_xx.setImageResource(R.mipmap.gyss_xiala_img);
            if (view.getId() == R.id.msgAll) {
                state = "";
                selectTv.setText("全部消息");
                img1.setVisibility(View.VISIBLE);
                img2.setVisibility(View.GONE);
                img3.setVisibility(View.GONE);
            } else if (view.getId() == R.id.msgWd) {
                state = "0";
                selectTv.setText("未读");
                img1.setVisibility(View.GONE);
                img2.setVisibility(View.VISIBLE);
                img3.setVisibility(View.GONE);
            } else if (view.getId() == R.id.msgYd) {
                state = "1";
                selectTv.setText("已读");
                img1.setVisibility(View.GONE);
                img2.setVisibility(View.GONE);
                img3.setVisibility(View.VISIBLE);
            }
            ((PushFragment)arrayList.get(vp.getCurrentItem())).search();
        }
    }
    public String getState(){
        return state;
    }
    public String getKey(){
        return searchEdit.getText().toString();
    }
}
